package com.lifeverse.event;

import com.lifeverse.entity.ConsciousnessState;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

import java.time.LocalDateTime;

/**
 * 意识状态变化事件
 */
@Getter
public class ConsciousnessStateChangeEvent extends ApplicationEvent {
    
    /**
     * 事件类型枚举
     */
    public enum EventType {
        STATE_CREATED("状态创建"),
        STATE_UPDATED("状态更新"),
        STATE_ENDED("状态结束"),
        STATE_ANOMALY_DETECTED("异常状态检测"),
        HIGH_INTENSITY_DETECTED("高强度状态检测"),
        LOW_QUALITY_DETECTED("低质量状态检测"),
        LONG_DURATION_DETECTED("长时间状态检测");
        
        private final String description;
        
        EventType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    private final EventType eventType;
    private final ConsciousnessState consciousnessState;
    private final ConsciousnessState previousState;
    private final LocalDateTime eventTimestamp;
    private final String eventDescription;
    private final Object eventData;
    
    /**
     * 构造函数 - 基本事件
     */
    public ConsciousnessStateChangeEvent(Object source, EventType eventType, ConsciousnessState consciousnessState) {
        super(source);
        this.eventType = eventType;
        this.consciousnessState = consciousnessState;
        this.previousState = null;
        this.eventTimestamp = LocalDateTime.now();
        this.eventDescription = eventType.getDescription();
        this.eventData = null;
    }
    
    /**
     * 构造函数 - 状态更新事件
     */
    public ConsciousnessStateChangeEvent(Object source, EventType eventType, ConsciousnessState consciousnessState, 
                                       ConsciousnessState previousState) {
        super(source);
        this.eventType = eventType;
        this.consciousnessState = consciousnessState;
        this.previousState = previousState;
        this.eventTimestamp = LocalDateTime.now();
        this.eventDescription = eventType.getDescription();
        this.eventData = null;
    }
    
    /**
     * 构造函数 - 完整事件
     */
    public ConsciousnessStateChangeEvent(Object source, EventType eventType, ConsciousnessState consciousnessState, 
                                       ConsciousnessState previousState, String eventDescription, Object eventData) {
        super(source);
        this.eventType = eventType;
        this.consciousnessState = consciousnessState;
        this.previousState = previousState;
        this.eventTimestamp = LocalDateTime.now();
        this.eventDescription = eventDescription != null ? eventDescription : eventType.getDescription();
        this.eventData = eventData;
    }
    
    /**
     * 获取生命体ID
     */
    public Long getLifeEntityId() {
        return consciousnessState != null ? consciousnessState.getLifeEntity().getId() : null;
    }
    
    /**
     * 判断是否为异常事件
     */
    public boolean isAnomalyEvent() {
        return eventType == EventType.STATE_ANOMALY_DETECTED || 
               eventType == EventType.LOW_QUALITY_DETECTED ||
               (consciousnessState != null && Boolean.TRUE.equals(consciousnessState.getIsAnomaly()));
    }
    
    /**
     * 判断是否为高优先级事件
     */
    public boolean isHighPriorityEvent() {
        return eventType == EventType.STATE_ANOMALY_DETECTED ||
               eventType == EventType.HIGH_INTENSITY_DETECTED ||
               eventType == EventType.LONG_DURATION_DETECTED;
    }
    
    /**
     * 获取事件摘要
     */
    public String getEventSummary() {
        StringBuilder summary = new StringBuilder();
        summary.append("事件类型: ").append(eventDescription);
        
        if (consciousnessState != null) {
            summary.append(", 生命体ID: ").append(consciousnessState.getLifeEntity().getId());
            summary.append(", 状态类型: ").append(consciousnessState.getStateType().getDescription());
            summary.append(", 强度级别: ").append(consciousnessState.getIntensityLevel());
        }
        
        summary.append(", 事件时间: ").append(eventTimestamp);
        
        return summary.toString();
    }
    
    @Override
    public String toString() {
        return "ConsciousnessStateChangeEvent{" +
                "eventType=" + eventType +
                ", consciousnessStateId=" + (consciousnessState != null ? consciousnessState.getId() : null) +
                ", lifeEntityId=" + getLifeEntityId() +
                ", eventTimestamp=" + eventTimestamp +
                ", eventDescription='" + eventDescription + '\'' +
                '}';
    }
}